package com.wcity.manage.service.community.volunteerservice.impl;

import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.volunteer.VolunteerVo;
import com.wcity.manage.dao.mapper.community.volunteer.VolunteerMapper;
import com.wcity.manage.dao.model.community.activity.Activity;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.dao.model.community.volunteer.Volunteer;
import com.wcity.manage.service.community.image.ImageService;
import com.wcity.manage.service.community.volunteerservice.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 志愿者服务
 * Created by hgg on 2016/12/20.
 */
@Service
public class VolunteerServiceImpl implements VolunteerService {

    @Autowired
    private VolunteerMapper volunteerMapper;
    @Autowired
    private ImageService imageService;

    /**
     * 志愿者服务列表
     * @param queryMap
     * @return
     */
    @Override
    public List<VolunteerVo> volunteerPageList( Map<String, Object> queryMap ) {

        return volunteerMapper.volunteerPageList(queryMap );
    }

    /**
     * 删除志愿者服务
     * @param deleteMap
     * @return
     */
    @Override
    public Integer deleteVolunteer( Map<String, Object> deleteMap ) {

        return volunteerMapper.deleteVolunteer( deleteMap );
    }

    /**
     * 新增志愿者服务
     * @param volunteer
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddVolunteer( Volunteer volunteer,String imgUrl,String blockIdStr ) {

        //【1】生成父级志愿者服务
        Integer parentId = 0;
        String[] blockIds = blockIdStr.split( "," );
        volunteer.setBlockId( Integer.parseInt( blockIds[0] ) );
        volunteer.setPid( 0 );
        Integer saveNum = volunteerMapper.saveAddVolunteer( volunteer );
        if( saveNum == 0 ){
            throw new RuntimeException( "新增志愿者服务失败" );
        }
        parentId = volunteer.getId();

        //【2】保存关联封面图片
        ImageUrl image = new ImageUrl( null,imgUrl,null,parentId, SaveImageType.COMMUNITY_VOLUNTEER );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存志愿者服务封面图片失败" );
        }

        //【3】执行保存志愿者服务
        saveAddVolunteer(volunteer, blockIds, parentId,imgUrl);
        return 1;
    }

    /**
     * 更新志愿者服务
     * @param volunteer
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveUpdateVolunteer( Volunteer volunteer,String imgUrl,String blockIdStr ) {

        Integer parentId = volunteer.getId();
        String[] blockIds = blockIdStr.split( "," );

        //【1】删除以前志愿者服务封面
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put( "referId", volunteer.getId() );
        deleteMap.put( "saveType", SaveImageType.COMMUNITY_VOLUNTEER );
        imageService.deleteImage( deleteMap );

        //【2】删除以前的志愿者服务
//        Map<String,Object> deleteVolunteerMap = new HashMap<>();
//        deleteVolunteerMap.put( "pid",volunteer.getId() );
//        deleteVolunteerMap.put( "userId",volunteer.getUserId() );
//        Integer deleteNum = volunteerMapper.deleteVolunteer( deleteVolunteerMap );
//        if( deleteNum == 0 ){
//            throw new RuntimeException( "删除志愿者服务失败" );
//        }

        //【3】执行保存志愿者服务封面
        ImageUrl image = new ImageUrl( null,imgUrl,null,volunteer.getId(),SaveImageType.COMMUNITY_VOLUNTEER );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存志愿者服务封面图片失败" );
        }

        //【4】执行更新志愿者服务
        volunteer.setBlockId( Integer.parseInt( blockIds[0] ) );
        Integer updateNum = volunteerMapper.saveUpdateVolunteer( volunteer );
        if( updateNum == 0 ){
            throw new RuntimeException( "更新志愿者服务失败 ");
        }

        //【5】执行新增志愿者服务
//        saveAddVolunteer(volunteer, blockIds, parentId,imgUrl);
        return 1;
    }

    /**
     * 志愿者服务详情
     * @param detailMap
     * @return
     */
    @Override
    public VolunteerVo volunteerDetail( Map<String, Object> detailMap ) {

        return volunteerMapper.volunteerDetail( detailMap );
    }

    /**
     * 志愿者服务总数量
     * @param queryMap
     * @return
     */
    @Override
    public Integer totalVolunteer( Map<String, Object> queryMap ) {

        return volunteerMapper.totalVolunteer( queryMap );
    }

    /**
     * 志愿者服务列表非分页
     * @param queryMap
     * @return
     */
    @Override
    public List<VolunteerVo> volunteerList( Map<String, Object> queryMap ) {

        return volunteerMapper.volunteerList( queryMap );
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    @Override
    public String blockIds( Map<String,Object> queryMap ){

        return volunteerMapper.blockIds( queryMap );
    }

    /**
     * 执行保存志愿者服务
     * @param volunteer
     * @param blockIds
     * @param parentId
     */
    private void saveAddVolunteer( Volunteer volunteer ,String[] blockIds,Integer parentId,String imgUrl ){

        for ( String id : blockIds ){
            volunteer.setBlockId( Integer.parseInt( id ) );
            //【1】执行保存
            volunteer.setPid( parentId );
            Integer saveNum = volunteerMapper.saveAddVolunteer( volunteer );
            if( saveNum == 0 ){
                throw new RuntimeException("保存志愿者服务失败");
            }

            //【2】执行保存志愿者服务封面
            ImageUrl image = new ImageUrl( null,imgUrl,null,volunteer.getId(),SaveImageType.COMMUNITY_VOLUNTEER );
            Integer saveImageNum = imageService.saveAddImage( image );
            if( saveImageNum == 0 ){
                throw new RuntimeException( "保存志愿者服务封面图片失败" );
            }
        }
    }
}
